package com.duoec.dw.lineage.core.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author xuwenzhen
 */
@Configuration
public class HiveDataSource {
    @Autowired
    private Environment env;

    @Bean(name = "hiveJdbcDataSource")
    @Qualifier("hiveJdbcDataSource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("hive.jdbc"));
        dataSource.setDriverClassName(env.getProperty("hive.driver-class-name"));
        dataSource.setUsername(env.getProperty("hive.user"));
        dataSource.setPassword(env.getProperty("hive.password"));
        dataSource.setTestWhileIdle(Boolean.parseBoolean(env.getProperty("hive.testWhileIdle")));
        dataSource.setValidationQuery(env.getProperty("hive.validationQuery"));
        dataSource.setMaxActive(Integer.parseInt(env.getProperty("hive.max-active", "5")));
        dataSource.setInitialSize(Integer.parseInt(env.getProperty("hive.initialSize", "3")));
        dataSource.setRemoveAbandoned(Boolean.parseBoolean(env.getProperty("hive.removeAbandoned", "true")));
        dataSource.setRemoveAbandonedTimeout(Integer.parseInt(env.getProperty("hive.removeAbandonedTimeout", "180")));
        return dataSource;
    }

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveJdbcDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
